<template>
  <BasicContainer>
    <ElRow>
      <ElForm
        ref="dataForm"
        :model="dataForm"
        :rules="dataRule"
        label-width="110px"
        @keyup.enter.native="dataFormSubmit()">
        <ElFormItem label="整改措施单号" prop="rectifyNum">
          <ElInput v-model="dataForm.rectifyNum" disabled placeholder="整改措施单号" />
        </ElFormItem>
        <ElFormItem label="描述" prop="describeText">
          <ElInput v-model="dataForm.describeText" type="textarea" rows="4" placeholder="描述" />
        </ElFormItem>
        <ElFormItem label="备注" prop="remarks">
          <ElInput v-model="dataForm.remarks" type="textarea" rows="4" placeholder="备注" />
        </ElFormItem>
        <!--        附件信息-->
        <ElFormItem label="附件信息">
          <UploadPage
            ref="uploadPage"
            :unique-code="dataForm.unid"
            :upload-permissions="permissionList"
            model="problem"
            classify="rectify" />
        </ElFormItem>
      </ElForm>
    </ElRow>
    <ElRow>
      <ElButton v-if="canSubmit" type="primary" @click="dataFormSubmit()">确定</ElButton>
    </ElRow>
  </BasicContainer>
</template>

<script>
  import { getObj, addObj, putObj } from '@/api/problem/feedback'
  import { getRectifyObj } from '@/api/problem/rectify'
  import { mapState } from 'vuex'
  import { getUUID } from '@/api/admin/uuid'

  export default {
    data() {
      return {
        canSubmit: false,
        dataForm: {
          id: 0,
          unid: '',
          createId: '',
          createBy: '',
          createTime: '',
          deptId: '',
          deptFullname: '',
          updateBy: '',
          updateTime: '',
          tenantId: '',
          delFlag: '',
          type: '',
          rectifyId: '',
          rectifyNum: '',
          status: '',
          submitDate: '',
          describeText: '',
          remarks: ''
        },
        dataRule: {
          rectifyId: [
            {
              required: true,
              message: '整改措施ID不能为空',
              trigger: 'blur'
            }
          ],
          rectifyNum: [
            {
              required: true,
              message: '整改措施单号不能为空',
              trigger: 'blur'
            }
          ]
        }
      }
    },
    computed: {
      ...mapState(['tag', 'permissions']),
      permissionList() {
        return {
          addBtn: this.$isEmpty(this.permissions.problem_feedback_add, false),
          delBtn: this.$isEmpty(this.permissions.problem_feedback_del, false),
          editBtn: this.$isEmpty(this.permissions.problem_feedback_edit, false),
          downloadBtn: this.$isEmpty(this.permissions.problem_feedback_download, false)
        }
      }
    },
    created() {
      this.init(0)
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        getRectifyObj(this.$route.params.id).then(res => {
          const data = res.data.data
          this.dataForm.rectifyNum = data.number
          this.dataForm.rectifyId = data.id
        })
        this.canSubmit = true
        this.$nextTick(() => {
          this.$refs.dataForm.resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              this.dataForm = response.data.data
            })
          } else {
            // 获取附件id
            getUUID().then(response => {
              this.dataForm.unid = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs.dataForm.validate(valid => {
          if (valid) {
            this.canSubmit = false
            if (this.dataForm.id) {
              putObj(this.dataForm)
                .then(data => {
                  this.$notify.success('修改成功')
                })
                .catch(() => {
                  this.canSubmit = true
                })
            } else {
              addObj(this.dataForm)
                .then(data => {
                  this.$notify.success('添加成功')
                })
                .catch(() => {
                  this.canSubmit = true
                })
            }
          }
        })
        this.$store.commit('DEL_TAG', this.tag)
        this.$router.push({ path: '/problem/feedback/index' })
      }
    }
  }
</script>
